﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class DoubleSideProgress : MonoBehaviour
{
    public UITouchObject leftHandle;
    public UITouchObject rightHandle;

    Vector2 handleLeftPos;
    Vector2 handleRightPos;

    public int minValue;
    public int maxValue;

    public int defauleLeftValue;
    public int defaultRightValue;

    public RectTransform fill;

    int m_curLeftHandleValue;
    public int curLeftValue => m_curLeftHandleValue;
    int m_curRightHandleValue;
    public int curRightValue => m_curRightHandleValue;

    public Action<int> onLeftValueUpdate;
    public Action<int> onRightValueUpdate;

    Vector2 m_LeftHandleTouchDownPos;
    Vector2 m_RightHandleTouchDownPos;
    Vector2 m_TouchDownPos;
    //bool touchingLeftHandle;
    float minPosX => handleLeftPos.x;
    float maxPosX => handleRightPos.x;
    float sliderLength => maxPosX - minPosX;

    float minDis = 20;

    public float moveFactor = 0.5f;

    void Awake()
    {
        leftHandle.onPointerDownBegin = OnLeftHandleTouchDown;
        leftHandle.onDrag = OnLeftHandleDrag;
        leftHandle.onPointerUp = OnLeftHandleTouchUp;
        rightHandle.onPointerDownBegin = OnRightHandleTouchDown;
        rightHandle.onDrag = OnRightHandleDrag;
        rightHandle.onPointerUp = OnRightHandleTouchUp;

        handleLeftPos = leftHandle.GetComponent<RectTransform>().anchoredPosition;
        handleRightPos = rightHandle.GetComponent<RectTransform>().anchoredPosition;

    }
    void Start()
    {

    }
    public void Init(int leftValue,int rightValue)
    {
        var leftHandlePos = handleLeftPos;
        leftHandlePos.x = GetHandlePosWithValue(leftValue);
        SetLeftHandlePos(leftHandlePos);

        var rightHandlePos = handleRightPos;
        rightHandlePos.x = GetHandlePosWithValue(rightValue);
        SetRightHandlePos(rightHandlePos);

        SetLeftValue(leftValue);
        SetRightValue(rightValue);
    }

    private void OnLeftHandleTouchDown(Vector2 vector)
    {
        m_TouchDownPos = vector; 
       // touchingLeftHandle = true;
        m_LeftHandleTouchDownPos = leftHandle.GetComponent<RectTransform>().anchoredPosition;

        GameDebug.Log($"OnLeftHandleTouchDown:{vector}");
    }
    private void OnLeftHandleDrag(Vector2 vector)
    {
        //GameDebug.Log($"OnLeftHandleDrag:{vector}");

        var vec = (vector - m_TouchDownPos) * moveFactor;
        var pos = m_LeftHandleTouchDownPos;
        pos.x = m_LeftHandleTouchDownPos.x + vec.x;

        SetLeftHandlePos(pos);

        int value = GetValueWithHandlePos(pos.x);// (int)Mathf.Lerp(minValue, maxValue, (pos.x - handleLeftPos.x) / sliderLength);

        SetLeftValue(value);
    }

    int GetValueWithHandlePos(float posX)
    {
        int value = (int)Mathf.Lerp(minValue, maxValue, (posX - handleLeftPos.x) / sliderLength);
        return value;
    }
    float GetHandlePosWithValue(int value)
    {
        float rate = (value - minValue) / (float)(maxValue - minValue);
        float posX = handleLeftPos.x + sliderLength * rate;
        return posX;
    }

    void SetLeftValue(int value)
    {
        if (this.m_curLeftHandleValue != value)
        {
            m_curLeftHandleValue = value;

            onLeftValueUpdate?.Invoke(value);
        }
    }

    private void OnRightHandleTouchDown(Vector2 pos)
    {
        m_TouchDownPos = pos;
        m_RightHandleTouchDownPos = rightHandle.GetComponent<RectTransform>().anchoredPosition;

        GameDebug.Log($"OnRightHandleTouchDown:{pos}");
    }
    private void OnRightHandleDrag(Vector2 vector)
    {
        //GameDebug.Log($"OnLeftHandleDrag:{vector}");

        var vec = (vector - m_TouchDownPos) * moveFactor;
        var pos = m_RightHandleTouchDownPos;
        pos.x = m_RightHandleTouchDownPos.x + vec.x;

        SetRightHandlePos(pos);

        int value = GetValueWithHandlePos(pos.x);
        SetRightValue(value);
    }
    void SetRightValue(int value)
    {
        if (this.m_curRightHandleValue != value)
        {
            m_curRightHandleValue = value;

            onRightValueUpdate?.Invoke(value);
        }
    }

    void SetLeftHandlePos(Vector2 pos)
    {
        float maxPos = rightHandle.GetComponent<RectTransform>().anchoredPosition.x - minDis;
        pos.x = Mathf.Clamp(pos.x, minPosX, maxPos);
        leftHandle.GetComponent<RectTransform>().anchoredPosition = pos; 
        UpdateFillRect();
    }
    void SetRightHandlePos(Vector2 pos)
    {
        float minPos = leftHandle.GetComponent<RectTransform>().anchoredPosition.x + minDis;
        pos.x = Mathf.Clamp(pos.x, minPos, maxPosX);
        rightHandle.GetComponent<RectTransform>().anchoredPosition = pos;
        UpdateFillRect();
    }

    void UpdateFillRect()
    {
        Vector2 leftHandlePos = leftHandle.GetComponent<RectTransform>().anchoredPosition;
        Vector2 rightHandlePos = rightHandle.GetComponent<RectTransform>().anchoredPosition;

        fill.anchoredPosition = (leftHandlePos + rightHandlePos) / 2;
        fill.sizeDelta = new Vector2((rightHandlePos - leftHandlePos).x, fill.sizeDelta.y);

    }

    private void OnLeftHandleTouchUp(Vector2 vector)
    {
        //touchingLeftHandle = false;
    }
    private void OnRightHandleTouchUp(Vector2 vector)
    {
        //touchingLeftHandle = false;
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}
